package com.stockz.equity.charting.painter.chart;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;

import org.springframework.stereotype.Component;

import com.stockz.equity.charting.util.MathUtil;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.model.Bar;
import com.stockz.equity.pattern.Painter;

@Component
public class RectangleVolumePainter implements Painter<StockChart> {
	
	@Override
	public void paint(Graphics2D g, StockChart view, Rectangle rectangle) {
		g.setColor(view.getStockChartPanel().getStockChartPanelSkin().getVolumeColor());

		StockChartPanelState state = view.getStockChartPanel().getStockChartPanelState();
		List<Bar> barList = state.getBarList();
		for(int i = state.getEndIndex()-1; i >= state.getStartIndex(); i--){
			Bar bar = barList.get(i);
			int x = MathUtil.fitX(rectangle.x, state.getTimeUnitSpace(), state.getEndIndex(), i);
			
			int volumeY = MathUtil.fitY(rectangle.y, 
					rectangle.y+rectangle.height, 
					state.getMinimumVolumeVisible(), 
					state.getMaximumVolumeVisible(), 
					bar.getVolume(),
					state.isSemiLog());
			
			g.fillRect(
					x - state.getTimeUnitSpace()/2,
					volumeY,
					state.getTimeUnitSpace(),
					rectangle.y+rectangle.height-volumeY);
		}
	}
}
